package com.kch.web.controller.system;

import com.kch.common.annotation.Log;
import com.kch.common.core.controller.BaseController;
import com.kch.common.core.domain.AjaxResult;
import com.kch.common.core.page.TableDataInfo;
import com.kch.common.enums.BusinessType;
import com.kch.framework.util.ShiroUtils;
import com.kch.system.domain.Meeting;
import com.kch.system.domain.MeetingApplicants;
import com.kch.system.domain.SysUser;
import com.kch.system.req.CommonReq;
import com.kch.system.service.IMeetingApplicantsService;
import com.kch.system.service.IMeetingService;
import com.kch.system.service.ISysTagService;
import com.kch.system.vo.MeetingMemberVO;
import com.kch.system.vo.MeetingVO;
import com.kch.web.config.CloumnProperties;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Controller
@RequestMapping("/system/meeting")
@Api(tags = "会议管理")
public class MeetingController extends BaseController {
    private String prefix = "system/meeting";
    @Autowired
    private IMeetingService meetingService;
    @Autowired
    private ISysTagService sysTagService;
    @Autowired
    private CloumnProperties cloumnProperties;
    @Autowired
    private IMeetingApplicantsService meetingApplicantsService;

    @RequiresPermissions("system:meeting:view")
    @GetMapping()
    public String index(ModelMap mmap) {
        List<String> collect = Stream.of(cloumnProperties.getMeeting()).collect(Collectors.toList());
        mmap.put("tags", sysTagService.selectByCloumnCode(collect));
        return prefix + "/meeting";
    }

    @PostMapping("/list")
    @RequiresPermissions("system:meeting:list")
    @ResponseBody
    public TableDataInfo list(Meeting meeting) {
        startPage();
        List<MeetingVO> list = meetingService.selectMeetingPage(meeting);
        return getDataTable(list);
    }

    @GetMapping("/status")
    @RequiresPermissions("system:meeting:list")
    @ResponseBody
    public AjaxResult status(Integer id) {
        try {
            meetingService.updateStatus(id);
            return toAjax(true);
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }


    @GetMapping("/add")
    public String add(ModelMap mmap) {
        List<String> collect = Stream.of(cloumnProperties.getMeeting()).collect(Collectors.toList());
        mmap.put("tags", sysTagService.selectByCloumnCode(collect));
        return prefix + "/add";
    }

    @Log(title = "会议", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult add(@Validated Meeting meeting) {
        meeting.setCreateBy(ShiroUtils.getLoginName());
        meeting.setCloumnCode(cloumnProperties.getMeeting());
        meetingService.insertSysMeeting(meeting);
        return toAjax(true);
    }

    @Log(title = "会议类型", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        try {
            return toAjax(meetingService.deleteMeeting(ids));
        } catch (Exception e) {
            return error(e.getMessage());
        }
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Integer id, ModelMap mmap) {
        MeetingVO meeting = meetingService.selectById(id);
        List<String> collect = Stream.of(cloumnProperties.getMeeting()).collect(Collectors.toList());
        mmap.put("tags", sysTagService.selectByCloumnCode(collect));
        mmap.put("meeting", meeting);
        return prefix + "/edit";
    }

    @Log(title = "会议", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated MeetingVO meetingVO) {
        try {
            meetingVO.setUpdateBy(ShiroUtils.getLoginName());
            meetingVO.setCloumnCode(cloumnProperties.getMeeting());
            return toAjax(meetingService.updateById(meetingVO));
        } catch (Exception e) {
            e.printStackTrace();
            return toAjax(false);
        }
    }

    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap mmap) {
        MeetingVO meeting = meetingService.selectById(id);
        mmap.put("meeting", meeting);
        return prefix + "/detail";
    }

    @GetMapping("/member/{id}/{type}")
    public String meetingMember(@PathVariable("id") Integer id, @PathVariable("type") String type,
                                ModelMap mmap) {
        mmap.put("id", id);
        mmap.put("type", type);
        return prefix + "/member";
    }

    @PostMapping("/member/list")
    @ResponseBody
    public TableDataInfo memberList(@RequestParam("id") Integer id, @RequestParam("type") String type,
                                    MeetingApplicants applicant) {
        MeetingApplicants applicants = new MeetingApplicants();
        if ("1".equals(type)) {
            applicants.setReviewStatus(type);
        }
        applicants.setMeetingId(id);
        if (!ObjectUtils.isEmpty(applicant.getReviewStatus())) {
            applicants.setReviewStatus(applicant.getReviewStatus());
        }
        startPage();
        List<MeetingMemberVO> list = meetingApplicantsService.selectByMeetingId(applicants);
        return getDataTable(list);
    }

    @GetMapping("/member/audit/{id}")
    public String auditMember(@PathVariable("id") Integer id, ModelMap mmap) {
        mmap.put("id", id);
        return prefix + "/audit";
    }

    @PostMapping("/member/audit")
    @ResponseBody
    public AjaxResult auditMember(MeetingApplicants applicants) {
        applicants.setCreateBy(ShiroUtils.getLoginName());
        meetingApplicantsService.updateById(applicants);
        return AjaxResult.success();
    }

    @PostMapping("/application")
    @ResponseBody
    @ApiOperation("会议报名,参数为会议记录id")
    public AjaxResult applicationMeeting(@RequestBody @Valid CommonReq req) {
        SysUser sysUser = ShiroUtils.getSysUser();
        meetingApplicantsService.application(req, sysUser);
        return AjaxResult.success();
    }
}
